package com.lintf.web.controller.weChat;

import com.lintf.common.constant.Constants;
import com.lintf.common.core.domain.AjaxResult;
import com.lintf.common.core.domain.model.WxLoginBody;
import com.lintf.framework.web.service.SysLoginService;
import com.lintf.system.service.wechat.IWeChatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @ system:Mac
 * @ author:lin tingfeng
 * @ data:2023/12/11 16:30
 * @ package_name:com.lintf.common.utils.weChat
 * @ project_name:mobile-project
 * @ desc:
 */
@Slf4j
@RestController
public class WeChatController {

    @Autowired
    private SysLoginService sysLoginService;
    @Autowired
    private IWeChatService weChatService;

    @PostMapping("wxLogin")
    public AjaxResult wxLogin(@RequestBody WxLoginBody wxLoginBody) {
        Map<String, Object> map = weChatService.wxLogin(wxLoginBody);
        String token = sysLoginService.wxLogin(map.get("decryptResult").toString(), map.get("openid").toString(), wxLoginBody.getUserDetail());
        return AjaxResult.success(Constants.TOKEN, token);
    }

//    推荐使用前端解码手机号然后赋值后传递给后端接口进行储存
//    /**
//     * 获取手机号：后端处理方式
//     */
//    @PostMapping("/getPhoneNumberByCode")
//    private String getPhoneNumber(String code) {
//        String phoneNumber = "";
//        String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + getAccessToken();
//        HashMap<String, String> params = new HashMap<>();
//        params.put("code", code);
//        try {
//            ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, params, String.class);
//            System.out.println(stringResponseEntity);
//            String resultString = stringResponseEntity.toString();
//            log.info("获取微信手机号码：{}", resultString);
//            if (StringUtils.isNotEmpty(resultString)) {
//                JSONObject jsonObject = JSON.parseObject(resultString);
//                JSONObject phone_info = jsonObject.getJSONObject("phone_info");
//                phoneNumber = phone_info.getString("phoneNumber");
//            } else {
//                log.error("返回值为空，请检查请求报文或者请求地址是否正确");
//            }
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return phoneNumber;
//    }
//
//    /**
//     * 获取手机号：后端处理方式
//     */
//    @PostMapping("/getPhoneNumber")
//    public Object getPhoneNumber(@RequestBody Map<String, Object> data) {
//        //通过appid和secret来获取token
//        //WXContent.APPID是自定义的全局变量
//        String tokenUrl = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", appid, appSecret);
//        String res = restTemplate.getForObject(tokenUrl, String.class);
//        JSONObject token = JSONObject.parseObject(res);
//
//        //通过token和code来获取用户手机号
//        String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + token.getString("access_token");
//
//        //封装请求体
//        Map<String, String> paramMap = new HashMap<>();
//        paramMap.put("code", data.get("code").toString());
//
//        //封装请求头
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_JSON);
//
//        HttpEntity<Map<String, String>> httpEntity = new HttpEntity<>(paramMap, headers);
//
//        //通过RestTemplate发送请求，获取到用户手机号码
//        RestTemplate restTemplate = new RestTemplate();
//        ResponseEntity<Object> response = restTemplate.postForEntity(url, httpEntity, Object.class);
//
//        //返回到前端展示
//        return response.getBody();
//    }


}
